{% template "admin/header.html" %}
<link rel="stylesheet" href="/static/plugins/zTree/css/zTreeStyle.css" type="text/css">
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field">
        <legend>配置权限</legend>
        <div class="layui-field-box">
            <form class="layui-form layui-form-pane">
                <ul id="treeDemo" class="ztree"></ul>
                <div class="layui-form-item text-center">
                    <button type="button" class="layui-btn" lay-submit="" lay-filter="submit">提交保存</button>
                    <button class="layui-btn layui-btn-danger" type="button" onclick="window.history.back()">返回</button>
                </div>
            </form>
        </div>
    </fieldset>
</div>

{% template "admin/footer.html" %}
<script type="text/javascript" src="/static/common/js/jquery.2.1.1.min.js"></script>
<script type="text/javascript" src="/static/plugins/zTree/js/jquery.ztree.core.min.js"></script>
<script type="text/javascript" src="/static/plugins/zTree/js/jquery.ztree.excheck.min.js"></script>

<script>

    var setting = {
        check:{enable: true},
        view: {showLine: false, showIcon: false, dblClickExpand: false},
        data: {
            simpleData: {enable: true, pIdKey:'pid', idKey:'id'},
            key:{name:'title'}
        }
    };
    var zNodes = {};
    function setCheck() {
        var zTree = $.fn.zTree.getZTreeObj("treeDemo");
        zTree.setting.check.chkboxType = { "Y":"ps", "N":"ps"};
    }

    var checkeds = [];
    function in_array(gid) {
        for (var i=0;i<checkeds.length;i++){
            if(checkeds[i] == gid){
                return true;
            }
        }
        return false;
    }
    function getTree(data){
        data.push({id:0,pid:0,title:"全部",open:true});
        for (var i=0;i<data.length;i++){
            data[i].open = true;
            if(in_array(data[i].id)){
                data[i].checked = true;
            }
        }

        $.fn.zTree.init($("#treeDemo"), setting, data);
        setCheck();
    }

    var id = getUrlParam("id");
    layui.use(['form', 'layer'], function () {
        var form = layui.form, layer = layui.layer;
        let loading =layer.load(1, {shade: [0.1,'#fff']});

        $.post('/admin/menu/query',function (md) {

            data = [];
            for (var i=0;i<md.Data.length;i++){
                data.push(upperJSONKey(md.Data[i]))
            }

            $.post('/admin/group/getAccess',{group_id:id},function (res) {
                layer.close(loading);
                if(res.Data){
                    for (var i=0;i<res.Data.length;i++){
                        checkeds.push(res.Data[i].Menu_id);
                    }
                }
                getTree(data);

            });
        });



        form.on('submit(submit)', function () {
            loading =layer.load(1, {shade: [0.1,'#fff']});
            // 提交到方法 默认为本身
            var treeObj=$.fn.zTree.getZTreeObj("treeDemo"),
                nodes=treeObj.getCheckedNodes(true);

            var ids = [];
            for (var i=0;i<nodes.length;i++) {
                ids.push(nodes[i].id);
            }

            $.post("/admin/group/updateAccess", {menu_id:ids.join(','),group_id:id}, function (res) {
                layer.close(loading);
                if (res.Code > 0) {
                    layer.msg(res.Msg, {time: 1800, icon: 1}, function () {
                        window.history.back();
                    });
                } else {
                    layer.msg(res.Msg, {time: 1800, icon: 2});
                }
            });
        })
    });
</script>
</body>
</html>
